<?php
class MapAction extends Action{
    public function index() {
        $model = M('polyline');
        $list = $model->where('1=1')->select();
        foreach($list as &$linevo){
            $linevo['ext']= json_decode($linevo['plPath'],true);
            $linevo['style']= json_decode($linevo['plOpts'],true);
        }
        $this->pagetitle = '地图总揽';
        $this->assign('list',$list);
        $this->display();
    }
    public function line_list() {
        $model = M('polyline');
        $list = $model->where('1=1')->select();

        $this->pagetitle = '线路列表';
        $this->assign('list',$list);
        $this->display();
    }
    public function line_delete($lineid=0){
        if($lineid>0){
            $model = M('polyline');
            $model->where('plId=%d',$lineid)->delete();
            $this->success('删除成功.',U('/Map'));
        }else{
            $this->error('错误的参数');
        }
    }
    public function line_edit($lineid=0){
        if($lineid>0){
            $model = M('polyline');
            $linevo = $model->where('plId=%d',$lineid)->find();
            $linevo['ext']= json_decode($linevo['plPath'],true);
            $linevo['style']= json_decode($linevo['plOpts'],true);
            $this->assign('linevo',$linevo);
            $model = M('polyline');
            $list = $model->where('1=1')->select();
            $this->assign('list',$list);
            $this->pagetitle = '线路编辑';
            $this->display();
        }else{
            $this->error('错误的参数');
        }
    }
    public function line_edit_post(){
        $title = $this->_param('title');
        $remark = $this->_param('remark');
        $ext = $this->_param('ext');
        $color = $this->_param('color');
        $lineid = $this->_param('id');
        if( empty($ext) || empty($color) || empty($title) || empty($remark) ){
            $this->error('错误的参数');
        }
        $styleOptions = array();
        $styleOptions['strokeColor'] = $color;
        $styleOptions['fillColor'] = $color;
        $line = base64_decode($ext,true);
        $vo['plTitle'] = $title;
        $vo['plRemark'] = $remark;
        $vo['plOpts'] = json_encode($styleOptions);
        $vo['plPath'] = $line;
        $model = M('polyline');
        $model->where('plId=%d',$lineid)->save($vo);
        //echo $model->getLastSql();
        $this->success('修改成功.',U('/Map/line_edit',array('lineid'=>$lineid)));
    }
    public function line_add(){
        $this->pagetitle = '添加一条新路径';
        $this->display();
    }
    public function line_add_post(){
        $title = $this->_param('title');
        $remark = $this->_param('remark');
        $ext = $this->_param('ext');
        $color = $this->_param('color');
        if( empty($ext) || empty($color) || empty($title) || empty($remark) ){
            $this->error('错误的参数');
        }
/*
        $styleOptions = {
            strokeColor:"red",    //边线颜色。
            fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
            strokeWeight: 3,       //边线的宽度，以像素为单位。
            strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
            fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
            strokeStyle: 'solid' //边线的样式，solid或dashed。
        }
*/
        $styleOptions = array(
            "strokeWeight"=>3,"strokeOpacity"=>0.8,"fillOpacity"=>0.6,"strokeStyle"=>'solid'
        );
        $styleOptions['strokeColor'] = $color;
        $styleOptions['fillColor'] = $color;
        $line = base64_decode($ext,true);
        $vo['plTitle'] = $title;
        $vo['plRemark'] = $remark;
        $vo['plOpts'] = json_encode($styleOptions);
        $vo['plPath'] = $line;
        $model = M('polyline');
        $model->add($vo);
        //echo $model->getLastSql();
        $this->success('添加成功.',U('/Map'));
    }

    public function test(){

        $styleOptions = array();
        $styleOptions['color'] = 'red';
        echo json_encode($styleOptions);
        echo '<br>';
    }
}